package com.itheima.ydd.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddCateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_cate")
public class YddCateController {

    @Autowired
    private IYddCateService iYddCateService;

    @Transactional
    @GetMapping("/del")
    public ResultData delete(Integer id) {

        iYddCateService.removeById(id);

        return ResultData.ok("删除成功", 0);
    }

    @Transactional
    @PostMapping("/update")
    public ResultData update(@RequestBody YddCate yddCate) {
        iYddCateService.updateById(yddCate);
        return ResultData.ok("修改成功", yddCate);
    }

    @PostMapping("/add")
    @Transactional
    public ResultData save(@RequestBody YddCate yddCate) {
        iYddCateService.save(yddCate);

        return ResultData.ok("添加分类成功", yddCate);
    }

    /**
     * 分页查询
     *
     * @param page     当前查询页码
     * @param limit    每页展示的数据
     * @param catename 可选参数
     * @return
     */
    @GetMapping("/list")
    public R yddcateList(int page, int limit, String catename) {
        //1.分页构造器
        Page<YddCate> catePage = new Page<>(page, limit);
        //2.条件构造器
        LambdaQueryWrapper<YddCate> lqw = new LambdaQueryWrapper<>();
        //3.条件判断
        lqw.eq(StringUtils.isNotEmpty(catename), YddCate::getCatename, catename);
        lqw.orderByAsc(YddCate::getSortNum);

        //4.调用分页信息进行查询
        Page<YddCate> yddCatePage = iYddCateService.page(catePage, lqw);

        //6.
        List<HashMap> hashMaps = yddCatePage.getRecords().stream().map(item -> {
            HashMap hashMap = JSON.parseObject(JSON.toJSONString(item), HashMap.class);
            hashMap.put("sort_num", hashMap.get("sortNum"));
            return hashMap;
        }).collect(Collectors.toList());


        Integer count = (int) yddCatePage.getTotal();//总数
        Integer totalPages = (int) yddCatePage.getPages();//总页数
//        long current = yddCatePage.getCurrent();
//        long size = yddCatePage.getSize();
        //5.返回结果
        R r = new R();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("data", hashMaps); //data
        map.put("count", count); //总数
        map.put("totalPages", totalPages);
        map.put("pageSize", limit);
        map.put("currentPage", page);
        r.setErrno(0);
        r.setData(map);
        r.setErrmsg("");
        return r;


    }

    /**
     * 分类列表全部
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData getAll() {

        List<YddCate> list = iYddCateService.list();
        return ResultData.ok("list", list);
    }

    /**
     * 分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo( Integer id) {
        //1.根据id查询
        YddCate yddCate = iYddCateService.getById(id);

        if (yddCate == null) {
            return ResultData.error("该分类不存在");
        }
        return ResultData.ok("yddCate", yddCate);

    }

}
